var img_top = null;
var img_mid = null;
var img_bot = null;
var img_center = null;
var menuepos = 0;
var active_obj = null;
var dragging = false;
var global_textIDs = 0;

function render_menue() {
	Presentation.imgP(img_center, pos.x, pos.y, 1/pos.scalar, pos.angle);
	if (menuepos == 0) {
		menue0();
	} else if (menuepos == 1) {
		menue1();
	} else if (menuepos == 2) {
		menue2();
	} else if (menuepos == 3) {
		menue3();
	} else if (menuepos == 4) {
		menue4();
	} else if (menuepos == 5) {
		menue5();
	}
	if (dragging == true && active_obj != null) {
		active_obj.x = Presentation.getX();
		active_obj.y = Presentation.getY();
		active_obj.angle = Presentation.getAngle();
		Helper.updateUnknown(active_obj);
	}
}

function init_menue() {
	img_top = Presentation.loadImage("menue_top.png");
	img_mid = Presentation.loadImage("menue_mid.png");
	img_bot = Presentation.loadImage("menue_bot.png");
	img_center = Presentation.loadImage("menue_center.png");
}

function menue_click(x,y,angle,scalar) {
	if ((50/pos.scalar)*50/pos.scalar > (x-pos.x)*(x-pos.x)+(y-pos.y)*(y-pos.y)) {
		/* center */
		menue_call(0);
	} else if (pos.x-100/pos.scalar+150/pos.scalar < x && pos.x+100/pos.scalar+150/pos.scalar > x && pos.y-30/pos.scalar-91/pos.scalar < y && pos.y+30/pos.scalar-91/pos.scalar > y) {
		/* top */
		menue_call(1);
	} else if (pos.x-100/pos.scalar+150/pos.scalar < x && pos.x+100/pos.scalar+150/pos.scalar > x && pos.y-30/pos.scalar-30/pos.scalar < y && pos.y+30/pos.scalar-30/pos.scalar > y) {
		/* upper */
		menue_call(2);
	} else if (pos.x-100/pos.scalar+150/pos.scalar < x && pos.x+100/pos.scalar+150/pos.scalar > x && pos.y-30/pos.scalar+31/pos.scalar < y && pos.y+30/pos.scalar+31/pos.scalar > y) {
		/* lower */
		menue_call(3);
	} else if (pos.x-100/pos.scalar+150/pos.scalar < x && pos.x+100/pos.scalar+150/pos.scalar > x && pos.y-30/pos.scalar+92/pos.scalar < y && pos.y+30/pos.scalar+92/pos.scalar > y) {
		/* bot */
		menue_call(4);
	} else {
	}
}

function menue_call(item) {
	if (item == 0) {
		if (menuepos == 0) {
			menuepos = 0;
			menue = false;
		} else if (menuepos == 1 || menuepos == 3 || menuepos == 5) {
			menuepos = 0;
		} else if (menuepos == 2) {
			menuepos = 0;
		} else if (menuepos == 4) {
			menuepos = 3;
		}
	} else if (item == 1) {
		if (menuepos == 0) {
			menuepos = 1;
		} else if (menuepos == 1) {
			menuepos = 2;
			active_obj = close_objects(item);
		} else if (menuepos == 2) {
			menue = false;
			menuepos = 0;
		} else if (menuepos == 3) {
			Helper.newText(pos.x, pos.y, "New Text "+global_textIDs, 30/pos.scalar, pos.angle);
			global_textIDs++;
			menuepos = 0;
			menue = false;
		} else if (menuepos == 4) {
			Helper.newBox(pos.x, pos.y, 300/pos.scalar, 50/pos.scalar, pos.angle);
			menuepos = 0;
			menue = false;
		} else if (menuepos == 5) {
			Helper.updateBackground("normal");
		}
	} else if (item == 2) {
		if (menuepos == 0) {
			menuepos = 3;
		} else if (menuepos == 1) {
			menuepos = 2;
			active_obj = close_objects(item);
		} else if (menuepos == 2) {
			menue = false;
			menuepos = 0;
			dragging = true;
		} else if (menuepos == 3) {
			menuepos = 4;
		} else if (menuepos == 4) {
			Helper.newCircle(pos.x, pos.y, 300/pos.scalar);
			menuepos = 0;
			menue = false;
		} else if (menuepos == 5) {
			Helper.updateBackground("clean");
		}
	} else if (item == 3) {
		if (menuepos == 0) {
			menuepos = 5;
		} else if (menuepos == 1) {
			menuepos = 2;
			active_obj = close_objects(item);
		} else if (menuepos == 2) {
			menue = false;
			menuepos = 0;
			Helper.remove(active_obj);
		} else if (menuepos == 3) {
			Helper.newImg("file.jpg", pos.x, pos.y, 1/pos.scalar, pos.angle);
			menuepos = 0;
			menue = false;
		} else if (menuepos == 4) {
			Helper.newLine(pos.x-100/pos.scalar, pos.y, pos.x+100/pos.scalar, pos.y);
			menuepos = 0;
			menue = false;
		} else if (menuepos == 5) {
			Helper.updateBackground("tech");
		}
	} else if (item == 4) {
		if (menuepos == 0) {
			menuepos = 0;
			menue = false;
			active_obj = null;
			dragging = false;
		} else if (menuepos == 1) {
			menuepos = 2;
			active_obj = close_objects(item);
		} else if (menuepos == 2) {
			menue = false;
			menue_pos = 0;
		} else if (menuepos == 3) {
			Helper.newVid("file", pos.x, pos.y, 1/pos.scalar, pos.angle, cur_step, cur_step);
			menuepos = 0;
			menue = false; 
		} else if (menuepos == 4) {
			menuepos = 0;
			menue = false;
		} else if (menuepos == 5) {
			Helper.updateBackground("fresh");
		}
	} else {
	}
}

/* gives the object closest to the current pos, distance gives not the closest, but the x-th element in the list of close objects */
function close_objects(distance) {
	return null;	
}

/* menue (root) */
function menue0() {
	Presentation.text(pos.x, pos.y,"EXIT", 30/pos.scalar, 0);
	Presentation.imgP(img_top, pos.x+150/pos.scalar, pos.y-91/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y-91/pos.scalar,"select", 30/pos.scalar, 0);
	Presentation.imgP(img_mid, pos.x+150/pos.scalar, pos.y-30/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y-30/pos.scalar,"add", 30/pos.scalar, 0);
	Presentation.imgP(img_mid, pos.x+150/pos.scalar, pos.y+31/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y+31/pos.scalar,"theme", 30/pos.scalar, 0);
	Presentation.imgP(img_bot, pos.x+150/pos.scalar, pos.y+92/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y+92/pos.scalar,"clear", 30/pos.scalar, 0);
}

/* menue->select */
function menue1() {
	Presentation.text(pos.x, pos.y,"BACK", 30/pos.scalar, 0);
	Presentation.imgP(img_top, pos.x+150/pos.scalar, pos.y-91/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y-91/pos.scalar,"1. Object", 30/pos.scalar, 0);
	Presentation.imgP(img_mid, pos.x+150/pos.scalar, pos.y-30/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y-30/pos.scalar,"2. Object", 30/pos.scalar, 0);
	Presentation.imgP(img_mid, pos.x+150/pos.scalar, pos.y+31/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y+31/pos.scalar,"3. Object", 30/pos.scalar, 0);
	Presentation.imgP(img_bot, pos.x+150/pos.scalar, pos.y+92/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y+92/pos.scalar,"4. Object", 30/pos.scalar, 0);
}

/* menue->select->object */
function menue2() {
	Presentation.text(pos.x, pos.y,"BACK", 30/pos.scalar, 0);
	Presentation.imgP(img_top, pos.x+150/pos.scalar, pos.y-30/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y-30/pos.scalar,"Modify", 30/pos.scalar, 0);
	Presentation.imgP(img_bot, pos.x+150/pos.scalar, pos.y+31/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y+31/pos.scalar,"Delete", 30/pos.scalar, 0);
}

/* menue->add */
function menue3() {
	Presentation.text(pos.x, pos.y,"BACK", 30/pos.scalar, 0);
	Presentation.imgP(img_top, pos.x+150/pos.scalar, pos.y-91/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y-91/pos.scalar,"text", 30/pos.scalar, 0);
	Presentation.imgP(img_mid, pos.x+150/pos.scalar, pos.y-30/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y-30/pos.scalar,"primitive", 30/pos.scalar, 0);
	Presentation.imgP(img_mid, pos.x+150/pos.scalar, pos.y+31/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y+31/pos.scalar,"image", 30/pos.scalar, 0);
	Presentation.imgP(img_bot, pos.x+150/pos.scalar, pos.y+92/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y+92/pos.scalar,"video", 30/pos.scalar, 0);
}

/* menue->add->primitive */
function menue4() {
	Presentation.text(pos.x, pos.y,"BACK", 30/pos.scalar, 0);
	Presentation.imgP(img_top, pos.x+150/pos.scalar, pos.y-91/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y-91/pos.scalar,"box", 30/pos.scalar, 0);
	Presentation.imgP(img_mid, pos.x+150/pos.scalar, pos.y-30/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y-30/pos.scalar,"circle", 30/pos.scalar, 0);
	Presentation.imgP(img_bot, pos.x+150/pos.scalar, pos.y+31/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y+31/pos.scalar,"line", 30/pos.scalar, 0);
}

/* menue->background */
function menue5() {
	Presentation.text(pos.x, pos.y,"BACK", 30/pos.scalar, 0);
	Presentation.imgP(img_top, pos.x+150/pos.scalar, pos.y-91/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y-91/pos.scalar,"default", 30/pos.scalar, 0);
	Presentation.imgP(img_mid, pos.x+150/pos.scalar, pos.y-30/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y-30/pos.scalar,"designer", 30/pos.scalar, 0); /* clean */
	Presentation.imgP(img_mid, pos.x+150/pos.scalar, pos.y+31/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y+31/pos.scalar,"technical", 30/pos.scalar, 0);
	Presentation.imgP(img_bot, pos.x+150/pos.scalar, pos.y+92/pos.scalar, 2/pos.scalar, 0);
	Presentation.text(pos.x+150/pos.scalar, pos.y+92/pos.scalar,"fresh", 30/pos.scalar, 0);
}
